package com.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.dao.CustomerDao;
import com.model.Customer;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class CustServerAction extends ActionSupport implements ModelDriven<Customer>{

	private Customer customer = new Customer();
	
	public Customer getModel() {
		return customer;
	}
	
	public String findCustmorByNum(){
		HttpServletRequest request=ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		
		String cnum = request.getParameter("cnum");
		
		CustomerDao dao = new CustomerDao();
		customer = dao.findCustomerByNum(cnum);
		
		if (customer == null) {
			session.setAttribute("cnum", cnum);
			return "newCustomer";
		} else {
			session.setAttribute("Customer", customer);
			return "ShowCustomer";

		}
		
		
	}
	
	public String addCustomer(){
		CustomerDao dao = new CustomerDao();
		dao.saveCustomer(customer);
		
		HttpServletRequest request=ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		session.setAttribute("Customer", customer);
		
		return "ShowCustomer";
	}


}
